package com.service;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.TRX_OUT;
import static com.def.GenericDef.T_XPANNOUN;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dao.IXpannounDao;
import com.icim.service.ICIMBaseService;
import com.model.Announcement;
import com.tx.Xpannoun.XpannounI;
import com.tx.Xpannoun.XpannounO;
import com.tx.Xpannoun.XpannounOA;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpannounService")
public class XpannounService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XpannounService.class);
	
	private static final String FIND_ALL = "L";
	private static final String FIND_BY_NO = "F";
	private static final String FIND_BY_UPDATE = "U";
	
	@Autowired
	private XpannounI xpannounI;
	
	@Autowired
	private XpannounO xpannounO; 

	@Autowired
	private XpannounOA xpannounOA; 
	
	@Autowired
	private MqOpe mqOpe;
	
	@Autowired
	private IXpannounDao announDao;
	
	public String subMainProc(String strInMsg){
		List<Announcement> announcementList = new ArrayList<Announcement>();
		
		try {
			xpannounI = (XpannounI) mqOpe.inStr2Obj(strInMsg,xpannounI);
			logger.info("InTrx is "+ strInMsg );
			
			if(xpannounI.getType().equals(FIND_ALL)){
				announcementList = announDao.loadAll();
			}else if(xpannounI.getType().equals(FIND_BY_NO)){
				announcementList.add(announDao.get(xpannounI.getAnnounce_no()));
			}else if(xpannounI.getType().equals(FIND_BY_UPDATE)){
				saveAnnouncement(xpannounI);
			}else{
				throw new Exception("Invalide type input: [" + xpannounI.getType() + "]");
			}
			
			if(!announcementList.isEmpty()){
				setOutTrxByList(announcementList);
			}
			
			xpannounO.setRtn_code(RETURN_CODE_OK);
		} catch (Exception e) {
			xpannounO.setRtn_code("0000001");
			xpannounO.setRtn_msg(e.getMessage());
		}finally{
			xpannounO.setTrx_id(T_XPANNOUN);
			xpannounO.setType_id(TRX_OUT);			
		}
		
		logger.info(mqOpe.obj2Str(xpannounO));
		return mqOpe.obj2Str(xpannounO);
	}
	
	public XpannounO setOutTrxByList(List<Announcement> announcementList){
		List<XpannounOA> xpannounOAList = new ArrayList<XpannounOA>();
		xpannounO.setCount(announcementList.size());
		for(Announcement announ: announcementList){
			XpannounOA xpannounOA = new XpannounOA();
			xpannounOA.setAnnounce_no(announ.getAnnounce_no());
			xpannounOA.setAnnounce_text(announ.getAnnounce_text());
			xpannounOA.setEvt_usr(announ.getEvt_usr());
			xpannounOA.setEvt_timestamp(announ.getEvt_timestamp());
			xpannounOAList.add(xpannounOA);
		}
		xpannounO.setOary(xpannounOAList);
		return xpannounO;
	}
	
	public void saveAnnouncement(XpannounI xpannounI){
		Announcement announcement = new Announcement();
		
		announcement.setAnnounce_no(xpannounI.getAnnounce_no());
		announcement.setEvt_usr(xpannounI.getEvt_usr());
		announcement.setEvt_timestamp(Util.getCurrentTimestamp());
		announcement.setAnnounce_text(xpannounI.getAnnounce_text());
		announDao.saveOrUpdate(announcement);
	}
	
}
